<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="应用名称" prop="appName">
        <el-input
          v-model="queryParams.appName"
          placeholder="请输入应用名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登陆类型" prop="loginType">
        <el-select v-model="queryParams.loginType" placeholder="请选择登陆类型" clearable>
          <el-option
            v-for="dict in dict.type.sso_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="登陆方法" prop="loginFunc">
        <el-select v-model="queryParams.loginFunc" placeholder="请选择登陆方法" clearable>
          <el-option
            v-for="dict in dict.type.sso_func"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:IndsvrSsoApp:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:IndsvrSsoApp:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:IndsvrSsoApp:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:IndsvrSsoApp:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="IndsvrSsoAppList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="应用ID" align="center" prop="id" />
      <el-table-column label="应用名称" align="center" prop="appName" />
      <el-table-column label="登陆类型" align="center" prop="loginType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sso_type" :value="scope.row.loginType"/>
        </template>
      </el-table-column>
      <el-table-column label="登陆方法" align="center" prop="loginFunc">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sso_func" :value="scope.row.loginFunc"/>
        </template>
      </el-table-column>

      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:IndsvrSsoApp:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:IndsvrSsoApp:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改应用对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="应用名称" prop="appName">
          <el-input v-model="form.appName" placeholder="请输入应用名称" />
        </el-form-item>
        <el-form-item label="登陆类型" prop="loginType">
          <el-select v-model="form.loginType" placeholder="请选择登陆类型">
            <el-option
              v-for="dict in dict.type.sso_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="登陆方法" prop="loginFunc" v-show="form.loginType==0">
          <el-select v-model="form.loginFunc" placeholder="请选择登陆方法">
            <el-option
              v-for="dict in dict.type.sso_func"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              v-if=""
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="被登陆方法" prop="loginFunc" v-show="form.loginType==99">
          <el-select v-model="form.loginFunc" placeholder="请选择被登陆方法">
            <el-option
              v-for="dict in dict.type.sso_func_passive"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              v-if=""
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="基础地址" prop="baseUrl"  v-show="form.loginType==0||form.loginType==99">
          <el-input v-model="form.baseUrl" placeholder="请输入基础地址" />
        </el-form-item>
        <el-form-item label="回调地址" prop="callbackUrl">
          <el-input v-model="form.callbackUrl" placeholder="请输入回调地址" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">授权信息信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddIndsvrSsoAppAuth">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteIndsvrSsoAppAuth">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="indsvrSsoAppAuthList" :row-class-name="rowIndsvrSsoAppAuthIndex" @selection-change="handleIndsvrSsoAppAuthSelectionChange" ref="indsvrSsoAppAuth">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="参数类型" prop="type" width="250">
            <template slot-scope="scope">
              <el-select v-model="scope.row.type" placeholder="请选择参数类型">
                <el-option
                  v-for="dict in dict.type.sso_auth_kv_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="键" prop="authKey" width="250">
            <template slot-scope="scope">
              <el-input v-model="scope.row.authKey" placeholder="请输入键" />
            </template>
          </el-table-column>
          <el-table-column label="值路径" prop="authValPath" width="250">
            <template slot-scope="scope">
              <el-input v-model="scope.row.authValPath" placeholder="请输入值路径" />
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listIndsvrSsoApp, getIndsvrSsoApp, delIndsvrSsoApp, addIndsvrSsoApp, updateIndsvrSsoApp } from "@/api/system/IndsvrSsoApp"

export default {
  name: "IndsvrSsoApp",
  dicts: ['sso_func', 'sso_type', 'sso_auth_kv_type','sso_func_passive'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedIndsvrSsoAppAuth: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应用表格数据
      IndsvrSsoAppList: [],
      // 授权信息表格数据
      indsvrSsoAppAuthList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        appName: null,
        loginType: null,
        loginFunc: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询应用列表 */
    getList() {
      this.loading = true
      listIndsvrSsoApp(this.queryParams).then(response => {
        this.IndsvrSsoAppList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        id: null,
        appName: null,
        loginType: null,
        loginFunc: null,
        baseUrl: null,
        callbackUrl: null
      }
      this.indsvrSsoAppAuthList = []
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加应用"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getIndsvrSsoApp(id).then(response => {
        this.form = response.data
        this.indsvrSsoAppAuthList = response.data.indsvrSsoAppAuthList
        this.open = true
        this.title = "修改应用"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.indsvrSsoAppAuthList = this.indsvrSsoAppAuthList
          if (this.form.id != null) {
            updateIndsvrSsoApp(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addIndsvrSsoApp(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除应用编号为"' + ids + '"的数据项？').then(function() {
        return delIndsvrSsoApp(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
	/** 授权信息序号 */
    rowIndsvrSsoAppAuthIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 授权信息添加按钮操作 */
    handleAddIndsvrSsoAppAuth() {
      let obj = {}
      obj.remark = ""
      obj.type = ""
      obj.authKey = ""
      obj.authValPath = ""
      this.indsvrSsoAppAuthList.push(obj)
    },
    /** 授权信息删除按钮操作 */
    handleDeleteIndsvrSsoAppAuth() {
      if (this.checkedIndsvrSsoAppAuth.length == 0) {
        this.$modal.msgError("请先选择要删除的授权信息数据")
      } else {
        const indsvrSsoAppAuthList = this.indsvrSsoAppAuthList
        const checkedIndsvrSsoAppAuth = this.checkedIndsvrSsoAppAuth
        this.indsvrSsoAppAuthList = indsvrSsoAppAuthList.filter(function(item) {
          return checkedIndsvrSsoAppAuth.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleIndsvrSsoAppAuthSelectionChange(selection) {
      this.checkedIndsvrSsoAppAuth = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/IndsvrSsoApp/export', {
        ...this.queryParams
      }, `IndsvrSsoApp_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
